/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.gui.facade;


import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


import fr.univ_tln.inf9.exaplanning.api.facade.event.AdminAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.AdminRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.BuildingAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.BuildingRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ComponentAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ComponentRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListAdminChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListBuildingChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListComponentChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListStudentChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.ListTeacherChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.StudentAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.StudentRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.TeacherAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.facade.event.TeacherRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.Building;
import fr.univ_tln.inf9.exaplanning.controleur.facade.UniversityController;
import fr.univ_tln.inf9.exaplanning.controleur.facade.UniversityView;

/**
 * @author pivi
 *
 */
public class JFrameListBuilding extends UniversityView implements ActionListener  {

	private JFrame frame = null;
	private JPanel contentPane = null;
	private JList listBuilding = null;
	private JLabel text1 = null;
	private JLabel textAdd = null;
	private TextField fieldAdd = null;
	private JScrollPane scrollBuilding = null;
	private DefaultListModel jListModel = null; 
	private static List<String> listzero = null;

	
	
	private JButton addButton;
	private JButton removeButton;
	
	public JFrameListBuilding(UniversityController controller) {
		this(controller, listzero);
	}
	
	public JFrameListBuilding(UniversityController controller, List<String> b){
		super(controller); 
		buildFrame(b);
	}
	
	private void buildFrame(List<String> b) {
		frame = new JFrame();
		
		contentPane = new JPanel();
		contentPane.setSize(20,30);
		contentPane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		/**
		 * En haut les la liste des batiment 
		 */
		
		jListModel = new DefaultListModel();
		
		
		text1 = new JLabel("Liste des batiments:");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 0;
		contentPane.add(text1);

		/**
		 * Insertion des éléments
		 */
		Iterator<String> it = b.iterator(); 
		while(it.hasNext()) {
			jListModel.addElement(it.next());
		}
		
		listBuilding = new JList(jListModel);
		listBuilding.setSize(40, 80);
		scrollBuilding = new JScrollPane(listBuilding);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 5;
		c.gridy = 0;
		contentPane.add(scrollBuilding);
		
		/**
		 * Ici les text field pour ajouter un truc
		 */
		
		textAdd = new JLabel("nom batiment :");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		contentPane.add(textAdd);
		
		fieldAdd = new TextField("          ");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		fieldAdd.setSize(new Dimension(50,10));
		contentPane.add(fieldAdd);
		

		/**
		 * Bas de page
		 */
		
		this.addButton = new JButton("ajouter Batiment");
		this.addButton.addActionListener(this);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 10;
		contentPane.add(addButton);
		this.removeButton = new JButton("enlever Batiment");
		this.removeButton.addActionListener(this);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 10;
		contentPane.add(removeButton);
		
		/*this.resultButton = new JButton("voir Batiment");
		this.resultButton.addActionListener(this);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 2;
		c.gridy = 10;
		//contentPane.add(resultButton);*/
		
		
		
		frame.setContentPane(contentPane);
		frame.setTitle("Tous les batîments :");
		frame.pack();
	}
	

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("ajouter Batiment")){
			Building b = null;
			try {
				b = new Building((String)this.fieldAdd.getText(),0,0);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.getController().notifyBuildingAddedToList(b);	
		}
		if(arg0.getActionCommand().equals("enlever Batiment")){
			this.getController().notifyBuildingRemovedToList((String)this.listBuilding.getSelectedValue());
		}
		
		/*if(arg0.getActionCommand().equals("voir Batiment")){
			
			Building b = new Building((String)this.listBuilding.getSelectedValue(),0,0,null);
			BuildingController controller2 = new BuildingController(b);
			controller2.displayViews();
		}
		*/
	}

	
	
	
	@Override
	public void ListBuildingChanged(ListBuildingChangedEvent event) {
		jListModel.addElement(event.getNewListBuilding());
		
	}

	@Override
	public void BuildingAddedToList(BuildingAddedToListEvent event) {
		jListModel.addElement(event.getNewBuilding());
	
	}

	@Override
	public void BuildingRemovedToList(BuildingRemovedToListEvent event) {
		jListModel.removeElement(event.getBuildingToRemove());
		
	}

	
	
	
	@Override
	public void close() {
		frame.dispose();
	}

	@Override
	public void display() {
		frame.setVisible(true);
	}

	
	
	/**
	 * Fonction Qu'on n'utilise pas
	 */
	
	@Override
	public void ListAdminChanged(ListAdminChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void AdminAddedToList(AdminAddedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void AdminRemovedToList(AdminRemovedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ListComponentChanged(ListComponentChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ComponentAddedToList(ComponentAddedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ComponentRemovedToList(ComponentRemovedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ListStudentChanged(ListStudentChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void StudentAddedToList(StudentAddedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void StudentRemovedToList(StudentRemovedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ListTeacherChanged(ListTeacherChangedEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void TeacherAddedToList(TeacherAddedToListEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void TeacherRemovedToList(TeacherRemovedToListEvent event) {
		// TODO Auto-generated method stub
		
	}




}
